package com.jiangjiang.eshop.purchase.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.purchase.dto.SupplierDTO;
import com.jiangjiang.eshop.purchase.dto.SupplierQueryDTO;
import com.jiangjiang.eshop.purchase.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Jianghaowen
 * @Date 2021/9/14
 * @Description: TODO
 * @Version V1.0
 **/
@Slf4j
@RestController
@RequestMapping("/purchase/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询供应商数据
     *
     * @param dto 查询条件
     * @return 查询结果
     */
    @PostMapping("/queryForPage")
    public Page<SupplierDTO> queryForPage(@RequestBody SupplierQueryDTO dto) {
        try {
            return supplierService.queryForPage(dto);
        } catch (Exception e) {
            log.warn("SupplierController queryForPage error", e);
        }
        return null;
    }

    /**
     * 根据id查询供应商数据
     *
     * @param id 供应商id
     * @return 查询结果
     */
    @GetMapping("/queryById/{id}")
    public SupplierDTO queryById(@PathVariable("id") Long id) {
        try {
            return supplierService.queryById(id);
        } catch (Exception e) {
            log.warn("SupplierController queryById error", e);
        }
        return null;
    }

    /**
     * 保存供应商数据
     *
     * @param dto 入参
     * @retur 处理结果
     */
    @PostMapping("/save")
    public Boolean save(@RequestBody SupplierDTO dto) {
        try {
            return supplierService.save(dto);
        } catch (Exception e) {
            log.warn("SupplierController save error", e);
        }
        return false;
    }
}
